// 构建映射关系
const fs = require('fs')
const shell = require('shelljs')
const inquirer = require('inquirer')
const path = require('path');
const current_path = path.resolve(__dirname);

const loop  = require('./loop.js')

const input_config = [
    {
        name: 'source_path',
        type: 'input',
        message: '源路径',
    },
    {
        name: 'template_path',
        type: 'input',
        message: '模板路径'
    },
    {
        name: 'out_path',
        type: 'input',
        message: '输出路径'
    }
    
]


function main (){

    let paths = {};    
    let input_promise = inquirer.prompt(input_config);
    
    input_promise.then( async answers =>{
        paths = Object.assign(paths, answers)
        paths = getResolve(paths);

        let file_source = JSON.parse(fs.readFileSync(paths.source_path));
        let file_template = JSON.parse(fs.readFileSync(paths.template_path));        
        let map = loop.creatMap(file_source, file_template);


        fs.writeFile(paths.out_path, JSON.stringify(map, null, 4), err =>{
            if(err){console.log(err)}

            shell.exec(`start ${paths.out_path}`)

        });

    })
}

function deepCopy(obj){
    return JSON.parse(JSON.stringify(obj))    
}

function getResolve(paths){
    paths = deepCopy(paths);

    Object.keys(paths).forEach(key =>{
        paths[key] = path.resolve(__dirname, paths[key])
    })

    return paths;
}


main();
